package com.yuntcm.group6.med_booking.controller;

import com.yuntcm.group6.med_booking.entity.Department;
import com.yuntcm.group6.med_booking.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//接收请求，返回结果
@CrossOrigin//允许跨域请求
@RestController//标记当前类是一个控制器类 返回结果为JSON数据结构
@RequestMapping("/department")//统一的前端，前端访问/department 开头的接口
public class DepartmentController {
    //找Service要数据
    @Autowired//注入Service，从容器中拿DepartmentService实现类对象
    //DepartmentServiceImpl
    private DepartmentService departmentService;

    //api:application+program+interface 应用程序编程接口
    @GetMapping//对应的是Get请求，访问“/department”路径时触发
    public List<Department> departmentList(){
        return departmentService.listALllDepartment();
    }

    //api
    @PostMapping("/updateStatus")
    public boolean updateStatus(@RequestBody Department department){
        boolean b = departmentService.updateById(department);
        return b;
    }

    //对应是Delete请求，访问“/department/delete”路径时触发此Api
    //@PathVariable获取访问路径中的变量
    @DeleteMapping("/delete/{id}")
    public boolean deleteById(@PathVariable Integer id){
        return departmentService.removeById(id);
    }

    //api:application+program+interface应用程序编程接口
    //对应是Post请求，访问“/department/add”路径时触发此Api
    @PostMapping("/add")
    public boolean add(@RequestBody Department department){
        return departmentService.save(department);
    }

    // 新增查询接口
    @GetMapping("/search")
    public List<Department> searchDepartments(@RequestParam String keyword) {
        return departmentService.listDepartmentsByName(keyword);
    }
}
